package net.sppan.base.controller.business;

import net.sppan.base.common.JsonResult;
import net.sppan.base.controller.BaseController;
import net.sppan.base.entity.business.Device;
import net.sppan.base.entity.business.Purchase;
import net.sppan.base.service.IDeviceService;
import net.sppan.base.service.IPurchaseService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 设备控制器
 *
 */
@Controller
@RequestMapping("/business/device")
public class DeviceController extends BaseController {

    @Autowired
    private IDeviceService deviceService;

    @RequestMapping(value = { "/", "/index" })
    public String index() {
        return "business/device/index";
    }

    /**
     * 设备列表查询
     * @param searchText
     * @return
     */
    @RequestMapping(value = {"/list"})
    @ResponseBody
    public Page<Device> list(@RequestParam(value = "searchText", required = false) String searchText) {

        Page<Device> page = deviceService.findAll(getPageRequest());
        return page;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap map) {
        return "business/device/form";
    }

    /**
     * 修改设备
     * @param device
     * @param map
     * @return
     */
    @RequestMapping(value= {"/edit"},method = RequestMethod.POST)
    @ResponseBody
    public JsonResult edit(Device device, ModelMap map){
        try {
            deviceService.update(device);
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

    /**
     * 页面跳转，返回页面，重定向页面
     * @param id
     * @param map
     * @return
     */
     @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String editId(@PathVariable Integer id, ModelMap map) {
        Device device = deviceService.find(id);
        map.put("device", device);
        return "business/device/form";
    }

    /**
     * 删除设备
     * @param id
     * @param map
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult delete(@PathVariable Integer id, ModelMap map) {
        try {
            deviceService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }


    @RequestMapping(value = {"/all"})
    @ResponseBody
    public List<Device> all() {

        List<Device> lists = deviceService.findAll();
        return lists;
    }
}
